<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form class="login_from" :model="loginFrom" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到甄选平台</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginFrom.username"> </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" v-model="loginFrom.password" show-password> </el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" class="login_btn" type="primary" size="default" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user';
import { getTime } from '@/utils/time';
import { Lock, User } from '@element-plus/icons-vue';
import { ElNotification } from 'element-plus';
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
let useStore = useUserStore()
// 获取路由器
let $router = useRouter()
// 获取路由对象
let $route = useRoute()
// 获取el-form组件
let loginForms = ref()
// 定义一个按钮loading状态
let loading = ref(false)
// 收集账号和密码
let loginFrom = reactive({ username: 'admin', password: '111111' })
// 登录按钮回调
const login = async () => {
  // 保证全部表单校验通过再发送登录请求
  await loginForms.value.validate()
  // 定义一个按钮开始加载效果
  loading.value = true

  // 点击登录按钮发送登录请求
  // 请求成功-》首页展示数据的地方
  // 请求失败-》弹出登录失败信息
  try {
    // 保证登陆成功
    await useStore.userLogin(loginFrom)
    // 编程式导航跳转到展示数据首页
    // 判断登录的时候，路由路径中是否有query参数，如果有就往query参数跳转，没有就跳转到首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // 登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎来到',
      title: `HI,${getTime()}好~`
    })
    // 登录loading效果成功也消失
    loading.value = false
  } catch (error) {
    //  登陆失败加载效果消失
    loading.value = false
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  }

}
// 定义表单校验对象rules
const rules = {
  // 规则对象属性
  username: [
    { required: true, message: '用户名不能为空', trigger: 'change' },
    { min: 3, max: 10, message: '账号长度至少是六位', trigger: 'change' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'change' },
    { min: 6, max: 15, message: '密码长度至少是六位', trigger: 'change' }
  ],
}


</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_from {
  position: relative;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png');
  background-size: cover;
  padding: 40px;
}

h1 {
  color: white;
  font-size: 40px;

}

h2 {
  font-size: 20px;
  color: white;
  margin: 20px 0px;
}

.login_btn {
  width: 100%;
}
</style>
